package vikaa.timelinepackage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import vikaa.timelinepackage.TimelineComment.CommentPostThread;
import vikaa.timelinepackage.TimelineComment.CommentWBPostThread;
import vikaa.timelinepackage.TimelineNewMemory.TwitterThread;
import vikaa.timelinepackage.TimelineNewMemory.postImageThread;
import vikaa.tools.DialogService;
import vikaa.tools.HttpUtil;
import vikaa.tools.Logger;
import vikaa.tools.StringUtils;
import vikaa.wowo.R;
import vikaa.wowo.cache.TimeLineSqlStoragePrivate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TimelineReply extends Activity{
	private LinearLayout backBtn;
	private EditText contentEdt;
	private ImageView submitBtn;
	private String content;
	private String hash;
	private String idstr;
	private String tip;
	private String twitter_type;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reply_comment);
		tip = getIntent().getExtras().getString("tip");
		hash = getIntent().getExtras().getString("hash");
		idstr = getIntent().getExtras().getString("idstr");
		twitter_type = getIntent().getStringExtra("twitter_type");
		contentEdt = (EditText)findViewById(R.id.contentEdt);
		contentEdt.setText(tip);
		contentEdt.setSelection(contentEdt.length());
		submitBtn = (ImageView)findViewById(R.id.submitBtn);
		submitBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				content = contentEdt.getText().toString();
				content = StringUtils.spaceEdit(content);
				if(content.length() < 1 )
					DialogService.WarnDialog(TimelineReply.this, getString(R.string.tip), getString(R.string.tips_comment_null), getString(R.string.ok));
				else if(content.indexOf(":") != -1){
					int index = content.indexOf(":");
					String sub =content.substring(index+1, content.length());
					sub = StringUtils.spaceEdit(sub);
					if(sub.length() < 1)
						DialogService.WarnDialog(TimelineReply.this, getString(R.string.tip), getString(R.string.tips_comment_null), getString(R.string.ok));
					else{
						content =content.substring(0, index+1) + sub;
						if(Timeline.dynamicFlag){
							new CommentWBPostThread(content, idstr, 10).start();
						}
						else{
							new CommentPostThread(content, hash, 5).start();
						}
					}
				}
				else{
					if(Timeline.dynamicFlag){
						new CommentWBPostThread(content, idstr, 10).start();
					}
					else{
						new CommentPostThread(content, hash, 5).start();
					}
				}
			}
		});
		backBtn = (LinearLayout)findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	
	Handler mhandler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 3:
				if(msg.obj != null){
					Toast.makeText(TimelineReply.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(TimelineReply.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 4:
				Toast.makeText(TimelineReply.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
				break;
			case 5:
				if(msg.obj != null){
					Comment_item item = (Comment_item) msg.obj;
					Intent in = new Intent();
					Bundle b = new Bundle();
					b.putString("avater", item.getAvater());
					b.putString("created_at", item.getCreated_at());
					b.putString("nickname", item.getNickname());
					b.putString("text", item.getText());
					b.putInt("msg", 5);
					in.putExtras(b);
					setResult(RESULT_OK, in);
					finish();
				}
				break;
			case 10:
				if(msg.obj != null){
					Comment_item item = (Comment_item) msg.obj;
					Intent in = new Intent();
					Bundle b = new Bundle();
					b.putString("avater", item.getAvater());
					b.putString("created_at", item.getCreated_at());
					b.putString("nickname", item.getNickname());
					b.putString("text", item.getText());
					b.putInt("msg", 10);
					in.putExtras(b);
					setResult(RESULT_OK, in);
					finish();
				}
				break;
			}
		}
    };
	class CommentWBPostThread extends Thread{
    	private String text;
    	private String idstr ;
    	private int m;
    	boolean isrun = true;
		public CommentWBPostThread(String text, String idstr , int m) {
			super();
			this.text = text;
			this.idstr  = idstr ;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("id", idstr);
    			map.put("text", text);
    			map.put("twitter_type", twitter_type);
    			try {
    				
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"comment/posttoweibo", map));
					if(jsonObj.getString("result").equals("1")){
						JSONObject jsonArr = jsonObj.getJSONObject("info");
							Comment_item item = new  Comment_item();
							item.setAvater(jsonArr.getString("avatar"));
							item.setCreated_at(jsonArr.getString("created_at"));
							item.setNickname(jsonArr.getString("nickname"));
							item.setText(jsonArr.getString("text"));
						Message msg = mhandler.obtainMessage(m);
						msg.obj = item;
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
    		}
    	}
    }
	class CommentPostThread extends Thread{
    	private String text;
    	private String hash;
    	private int m;
    	boolean isrun = true;
		public CommentPostThread(String text, String hash, int m) {
			super();
			this.text = text;
			this.hash = hash;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("hash", hash);
    			map.put("text", text);
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"comment/post", map));
					if(jsonObj.getString("result").equals("1")){
						Comment_item item = new Comment_item();
						//item.setAuthor(jsonObj.getJSONObject("info").getString("author"));
						item.setAvater(jsonObj.getJSONObject("info").getString("avatar"));
						item.setCreated_at(jsonObj.getJSONObject("info").getString("created_at"));
						item.setNickname(jsonObj.getJSONObject("info").getString("nickname"));
						item.setText(jsonObj.getJSONObject("info").getString("text"));
						Message msg = mhandler.obtainMessage(m);
						msg.obj = item;
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
    		}
    	}
    }
}
